<?php

namespace Web\MainSiteBundle\Controller;

use BaseExt\Pagination;
use Model\NoteBundle\Entity\Note;

use Model\NoteBundle\Model\NoteModel;
use Model\NoteReplyBundle\Model\NoteReplyModel;
use Model\CourseBundle\Entity\Course;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Method;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

/**
 * @route("Note")
 */
class NoteController extends WebBaseController
{
    /**
     * @route("/add",name="WebNoteAdd")
     */
    public function WebNoteAdd()
    {
        $user = $this->needToBeUser();
        if (!$whom = $this->getParamRequired("id")) {$this->showMessagePage("数据丢失，请刷新后重试");}
        if (!$content = $this->getParamRequired("content")) {$this->showMessagePage("请填写评论内容");}

        $params = array(
            "who"   => $user->getId(),
            "whom"  => $whom,
            "what"  => $content
        );
        $noteModel = new NoteModel($this->getDoctrine());
        $re = $noteModel->commonAdd($params);
        return $this->json_out(1,"添加成功",json_encode($re));
    }

    /**
     * @route("/reply/add",name="WebNoteReplyAdd")
     */
    public function WebNoteReplyAdd()
    {
        $user = $this->needToBeUser();
        if (!$whom = $this->getParamRequired("id")) {$this->showMessagePage("数据丢失，请刷新后重试");}
        if (!$content = $this->getParamRequired("content")) {$this->showMessagePage("请填写评论内容");}

        $params = array(
            "who"   => $user->getId(),
            "whom"  => $whom,
            "what"  => $content
        );
        $noteModel = new NoteReplyModel($this->getDoctrine());
        $re = $noteModel->commonAdd($params);
        return $this->json_out(1,"添加成功",json_encode($re));
    }

    /**
     * @route("/get/list/{uniqueId}",name="WebNoteList")
     */
    public function getListForLesson( Course $course){
        $this->needToBeUser();
        if (!$lessonId = $this->getParamRequired("id")) {$this->showMessagePage("参数丢失");}
        if (!$page = $this->getParamRequired("pn")) {$page = 1;}
        //获取评论
        $noteModel = new NoteModel($this->getDoctrine());
        $noteReplyModel = new NoteReplyModel($this->getDoctrine());
        $note = $noteModel->getListByLesson($lessonId,$page*10-10);
        foreach ($note as &$comment) {
            $comment["reply"] = $noteReplyModel->getListByWhom($comment["id"]);
        }
        $baseUrl = $this->generateUrl("WebCourseLessons",array("uniqueId"=>$course->getUniqueId(),"id"=>$lessonId,"pn"=>1));
        $data["courseNote"] = $note;
        $pageParam["per_page"] = "10";
        $pageParam["base_url"] = "";
        $pageParam["query_string_segment"] = "pn";
        $pageParam["first_url"] = $baseUrl."";
        $pageParam["total_rows"] = $noteModel->countNote($lessonId);
        $PageModel = new Pagination($pageParam);
        $page = $PageModel->create_links();
        $data["pageNote"] = $page;
        if (empty($note)) {
            return $this->json_out(0,"没有更多数据了","");
        } else {
            foreach ($note as &$comment) {
                $comment["reply"] = $noteReplyModel->getListByWhom($comment["id"]);
            }
            $data["courseNote"] = $note;
            $html = $this->renderView("WebMainSiteBundle:Course:Note/list.html.twig",$data);
            return $this->json_out(1,"列表获取成功",$html);
        }
    }

}
